/*
 * BanBu_HelpViewController
 * @description 本文件提供关于半圆界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供关于半圆界面的ViewController
 */

#import "BanBu_HelpViewController.h"
#import "BanBu_NavButton.h"
#import "BanBu_ImageView.h"
#import "BanBu_ThemeManager.h"
#import "UIBadgeView.h"
#import "AppDataManager.h"
#import "BanBu_NewVersionViewController.h"
#import "BanBu_FeedbackView.h"
#import "BanBu_UserAgreement.h"
#import "BanBu_UserHelpViewController.h"
#import "AppDataManager.h"
#import "BanBu_ChatViewController.h"
@interface BanBu_HelpViewController ()
{
    UIBadgeView *badgeView;
}
@end

@implementation BanBu_HelpViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)dealloc
{
    [super dealloc];
    [badgeView release],badgeView = nil;

}
-(void)popself
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    badgeView = [[UIBadgeView alloc] init];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, __MainScreen_Width, __MainScreen_Height-44) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

    
    
    
    BanBu_ImageView *img = [[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeTable];
    img.frame = CGRectMake(0, 0, 320, __MainScreen_Height-44);
    self.tableView.backgroundView = img;
    [img release];
//	// Do any additional setup after loading the view.
    
    self.title=NSLocalizedString(@"helpTitle", nil);
    
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)] autorelease];
    
    UILabel *banbu=[[UILabel alloc]initWithFrame:CGRectMake(0, 150, 320, 42)];
    
    banbu.backgroundColor=[UIColor clearColor];
    banbu.textAlignment = kTextAlignmentCenter;
    [headerView addSubview:banbu];
    
    [banbu release];
    
    banbu.numberOfLines=0;
    banbu.font = [UIFont boldSystemFontOfSize:16];
    banbu.text= NSLocalizedString(@"propagandaTitle", nil);
    
    // 图标
    
    UIImageView *iconImage=[[UIImageView alloc]initWithFrame:CGRectMake(100, 30,120 , 120)];
    
    iconImage.image=[UIImage imageNamed:@"logo.png"];
    iconImage.layer.cornerRadius = 20;
    iconImage.clipsToBounds = YES;
    [headerView addSubview:iconImage];
    
    [iconImage release];
    
    // uilable tips
    //版本
    NSString *developmentVersionNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    NSString *appVersion = developmentVersionNumber;
    UILabel *versionLabel = [[UILabel alloc]initWithFrame:CGRectMake(310-200, 0, 200, 30)];
    versionLabel.textAlignment = kTextAlignmentRight;
    versionLabel.backgroundColor = [UIColor clearColor];
    versionLabel.font = [UIFont systemFontOfSize:15];
    versionLabel.text = [NSString stringWithFormat:@"V%@",appVersion];
    [headerView addSubview:versionLabel];
    [versionLabel release];
    
    _tableView.tableHeaderView = headerView;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        cell.backgroundView = nil;
        
        cell.textLabel.backgroundColor = [UIColor clearColor];
    }
    cell.detailTextLabel.text = nil;
    //使用全局的getThemeImage来切换cell的背景图片，保证背景图片能够及时的改变，同时重写viewwillappear方法来保证每次的cell都要读取一下主题背景图片  a little keng
    if([ThemeManager getThemeImage:@"tablecell_backimage.png"])
        cell.backgroundColor = [UIColor colorWithPatternImage:[ThemeManager getThemeImage:@"tablecell_backimage.png"]];
    else
        cell.backgroundColor = nil;
    
        cell.accessoryView = nil;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        if(!indexPath.row)
        {
            cell.textLabel.text = NSLocalizedString(@"newVersionIntroduce", nil);
            NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
//            [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
            CGSize size = [NSLocalizedString(@"newVersionIntroduce", nil) sizeWithFont:[UIFont systemFontOfSize:18] constrainedToSize:CGSizeMake(1000, 40) lineBreakMode:kTextLineBreakByWordWrapping];
            
            if(![MyAppDataManager.newVersionNumber isEqual:version])
            {
                
                [badgeView removeFromSuperview];
                badgeView.frame = CGRectMake(size.width+10,10, 50, 20);
                badgeView.backgroundColor = [UIColor clearColor];
                badgeView.badgeColor = [UIColor redColor];
                badgeView.badgeString  = @"New";
                badgeView.tag = 1880;
                [cell.contentView addSubview:badgeView];
            }
            cell.detailTextLabel.textColor = [UIColor blueColor];
            cell.detailTextLabel.backgroundColor = [UIColor clearColor];
            cell.detailTextLabel.text = MyAppDataManager.newVersionNumber;
        }
//        else if(indexPath.row == 1)
//        {
//            cell.textLabel.text = NSLocalizedString(@"identityAuth", nil);
//        }
        else if(indexPath.row == 1)
        {
            cell.textLabel.text = NSLocalizedString(@"feedBackLabel", nil);
        }
        else if(indexPath.row == 2)
        {
            cell.textLabel.text = NSLocalizedString(@"userAgreeTitle", nil);
        }
        else if (indexPath.row == 3)
        {
            cell.textLabel.text = NSLocalizedString(@"sysAboutUserManual", nil);
        }
        else if (indexPath.row == 4)
        {
            cell.textLabel.text = NSLocalizedString(@"staff_online", nil);
        }
    return cell;

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    [self tableView:tableView didSelectRowAtIndexPath:indexPath];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    //新版本介绍
    if(!indexPath.row)
    {
        BanBu_NewVersionViewController *newVersion = [[BanBu_NewVersionViewController alloc] init];
        [self.navigationController pushViewController:newVersion animated:YES];
        [newVersion release];
        return;
    }
    //意见反馈
    else if(indexPath.row == 1)
    {
        //cell.textLabel.text = @"意见反馈";
        BanBu_FeedbackView *aFeed = [[BanBu_FeedbackView alloc]init];
        aFeed.navigationItem.title = NSLocalizedString(@"feedBackLabel", nil);
        [self.navigationController pushViewController:aFeed animated:YES];
        [aFeed release];
        return;
    }
    //用户协议
    else if(indexPath.row == 2)
    {
        //cell.textLabel.text = @"用户帮助";后改为“用户协议”
        BanBu_UserAgreement *agree = [[BanBu_UserAgreement alloc]init];
        [self.navigationController pushViewController:agree animated:YES];
        [agree release];
        return;
    }
    //用户帮助手册
    else if (indexPath.row == 3)
    {
        BanBu_UserHelpViewController *help = [[BanBu_UserHelpViewController alloc] init];
        [self.navigationController pushViewController:help animated:YES];
        [help release];
    }
    //在线客服
    else if (indexPath.row == 4)
    {
        NSString *langauage=[MyAppDataManager getPreferredLanguage];
        NSString * kefuId;
        //中国900客服
        if([langauage isEqualToString:@"zh-Hans"])
        {
            kefuId = @"900";
        }
        //日本700客服
        else if([langauage isEqualToString:@"ja"])
        {
            NSLog(@"700客服");
            kefuId = @"700";
        }
        //其他国家800客服
        else
        {
            kefuId = @"800";
        }

        
        //先获取本地存储的资料
        NSArray * userinfoArray = [MyAppDataManager getPersonInfo:@[kefuId]];
        NSDictionary * userInfoDic = nil;
        if (userinfoArray.count>0)
        {
            userInfoDic = [userinfoArray objectAtIndex:0];
            //获取之前的聊天记录
            [MyAppDataManager readTalkList:[userInfoDic valueForKey:@"userid"] WithNumber:5];
            BanBu_ChatViewController * chat = [[BanBu_ChatViewController alloc] initWithPeopleProfile:userInfoDic];
            chat.smileViewBackup = MyAppDataManager.smileViewBackup;
            //是否显示headerview
            if([[userInfoDic valueForKey:@"userid"] intValue]<1000)
            {
                chat.hidHeader = YES;
            }
            else
            {
                chat.hidHeader = NO;
            }

            [self.navigationController pushViewController:chat animated:YES];
            [chat release];
        }
        //如果本地未存储，从网上重新获取资料
        if (userInfoDic == nil)
        {
            //先获取资料
            NSDictionary * parDict = [NSDictionary dictionaryWithObjectsAndKeys:@"",@"fromtime",kefuId,@"userid", nil];
            
            //获取用户的扩展资料
            [AppComManager getBanBuData:BanBu_Get_User_info_v2 par:parDict delegate:self];
        }
    }

    
}
-(void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    if(error)
    {
        [TKLoadingView showTkloadingAddedTo:self.view title:NSLocalizedString(@"network_error", nil) activityAnimated:NO duration:1];
        
    }
    else if([AppComManager respondsDic:resDic isFunctionData:BanBu_Get_User_info_v2])
    {
        //其他操作
        NSMutableDictionary *userInfor = [NSMutableDictionary dictionaryWithDictionary:resDic];
        [userInfor removeObjectForKey:@"fc"];
        [userInfor removeObjectForKey:@"actioncount"];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:userInfor,keyPersonalInfo,[resDic valueForKey:@"userid"],@"userid",nil];
        
        __block NSMutableArray *saveArr = [[NSMutableArray alloc] initWithObjects:dic, nil];
        
        //向数据库存储个人信息，重新请求数据
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            [MyAppDataManager writePersonalDataToDB:saveArr];
            [saveArr release];
        });
        
        BanBu_ChatViewController * chat = [[BanBu_ChatViewController alloc] initWithPeopleProfile:resDic];
        chat.smileViewBackup = MyAppDataManager.smileViewBackup;
        [self.navigationController pushViewController:chat animated:YES];
        [chat release];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
